package com.graduate.bssp.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.graduate.base.controller.BaseController;
import com.graduate.base.entity.PageInfo;
import com.graduate.bssp.dao.UserMapper;
import com.graduate.bssp.entity.Comic;
import com.graduate.bssp.entity.Main;
import com.graduate.bssp.entity.Movie;
import com.graduate.bssp.entity.User;
import com.graduate.bssp.service.*;
import com.graduate.common.constants.MessageConstants;
import com.graduate.common.dto.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author yongqiangyao
 * @since 2018-02-05
 */
@Controller
@RequestMapping("/bssp/main")
public class MainController extends BaseController {
    @Autowired
    private IComicService comicService;
    @Autowired
    private IMovieService movieService;
    @Autowired
    private ITvshowService tvshowService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private IUserService userService;
    @Autowired
    private IMainService mainService;


    @RequestMapping("")
    public String hello() {
        return "views/main";
    }
    @RequestMapping("/adminLogin")
    public String adminLogin() {
        return "views/manage";
    }

    /**
     * 主页所有总页数，翻页功能在前端实现
     * @param pageInfo
     * @return
     */
    @RequestMapping("/page")
    @ResponseBody
    public AjaxResult pageData(PageInfo<Main> pageInfo) {
        AjaxResult ajaxResult = null;
        HashMap<String,Object> pageTotalList=new HashMap<>();
        try {
            Integer comicPageTotal = comicService.selectCountList();
            Integer moviePageTotal=movieService.selectCountList();
            Integer tvShowPageTotal=tvshowService.selectCountList();
            pageInfo.count();
            Integer hotTotPage=mainService.selectCountList();
            pageTotalList.put("hotTotPage",hotTotPage);
            pageTotalList.put("comicPageTotal",comicPageTotal);
            pageTotalList.put("moviePageTotal",moviePageTotal);
            pageTotalList.put("tvShowPageTotal",tvShowPageTotal);
            ajaxResult = json(MessageConstants.BSSP_STATUS_SUCCESS, pageTotalList);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult = result(MessageConstants.BSSP_STATUS_FAIL);
        }
        return ajaxResult;
    }

    /**
     * 主页所有总页数，翻页功能在前端实现
     * @param main
     * @return
     */
    @RequestMapping("/sortPage")
    @ResponseBody
    public AjaxResult sortPageData(Main main) {
        AjaxResult ajaxResult = null;
        HashMap<String,Object> pageTotalList=new HashMap<>();
        try {
            Integer sortPageTotal=mainService.selectTypeCount(main);
            pageTotalList.put("sortPageTotal",sortPageTotal);
            ajaxResult = json(MessageConstants.BSSP_STATUS_SUCCESS, pageTotalList);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult = result(MessageConstants.BSSP_STATUS_FAIL);
        }
        return ajaxResult;
    }

    /**
     * 热门电影内容
     * @param pageInfo
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public AjaxResult listData(PageInfo<Main> pageInfo) {
        AjaxResult ajaxResult = null;
        try {
            pageInfo.count();
            List<Main> mainContentList=mainService.selectMainList(pageInfo);
            ajaxResult = json(MessageConstants.BSSP_STATUS_SUCCESS, mainContentList);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult = result(MessageConstants.BSSP_STATUS_FAIL);
        }
        return ajaxResult;
    }
    /**
     * 热门电影内容
     * @param pageInfo
     * @return
     */
    @RequestMapping("/sortList")
    @ResponseBody
    public AjaxResult sortListData(PageInfo<Main> pageInfo) {
        AjaxResult ajaxResult = null;
        try {
            pageInfo.setpageNumber(20);
            pageInfo.count();
            List<Main> typeContentList=mainService.selectTypeList(pageInfo);
            ajaxResult = json(MessageConstants.BSSP_STATUS_SUCCESS, typeContentList);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult = result(MessageConstants.BSSP_STATUS_FAIL);
        }
        return ajaxResult;
    }

    /**
     * 搜索
     * @param pageInfo
     * @return
     */
    @RequestMapping("/search")
    @ResponseBody
    public AjaxResult searchListData(PageInfo<Main> pageInfo) {
        AjaxResult ajaxResult = null;
        try {
            pageInfo.setpageNumber(20);
            pageInfo.count();
            List<Main> searchList=mainService.selectSearchList(pageInfo);
            ajaxResult = json(MessageConstants.BSSP_STATUS_SUCCESS, searchList);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult = result(MessageConstants.BSSP_STATUS_FAIL);
        }
        return ajaxResult;
    }
 /**
     * 搜索
     * @param main
     * @return
     */
    @RequestMapping("/searchCount")
    @ResponseBody
    public AjaxResult searchListDataCount(Main main) {
        AjaxResult ajaxResult = null;
        try {
            int searchCount=mainService.selectSearchCount(main);
            ajaxResult = json(MessageConstants.BSSP_STATUS_SUCCESS, searchCount);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxResult = result(MessageConstants.BSSP_STATUS_FAIL);
        }
        return ajaxResult;
    }


}
